package com.chenwenxuan.sync.console.dao;

import java.util.List;

import com.chenwenxuan.sync.console.model.TaskCatalog;

public class TaskCatalogDao extends AbstractDao {
	
	public void addTaskCatalog(TaskCatalog taskCatalog) {
		getJdbcTemplate().update(addTaskCatalogSql, new Object[]{taskCatalog.getTaskCatalogId(), taskCatalog.getTaskCatalogName(), taskCatalog.getParentTaskCatalogId()});
	}
	
	public void updateTaskCatalog(TaskCatalog taskCatalog) {
		getJdbcTemplate().update(updateTaskCatalogSql, new Object[]{taskCatalog.getTaskCatalogName(), taskCatalog.getParentTaskCatalogId(), taskCatalog.getTaskCatalogId() });
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskCatalog> queryTaskCatalogsByParentCatalogId(int parentTaskCatalogId) {
		return (List<TaskCatalog>) getJdbcTemplate().query(queryTaskCatalogsSql, new Object[]{parentTaskCatalogId}, new RowMappers.TaskTypeUIRowMapper());
	}
	
	public TaskCatalog queryTaskCatalog(int taskCatalogId) {
		return (TaskCatalog) getJdbcTemplate().queryForObject(queryTaskCatalogSql, new Object[]{taskCatalogId}, new RowMappers.TaskTypeUIRowMapper());
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskCatalog> queryTaskCatalogs(int taskCatalogId) {
		return (List<TaskCatalog>) getJdbcTemplate().query(queryTaskCatalogSql, new Object[]{taskCatalogId}, new RowMappers.TaskTypeUIRowMapper());
	}
	
	public void deleteTaskCatalogSql(int taskCatalogId) {
		getJdbcTemplate().update(deleteTaskCatalogSql, new Object[]{taskCatalogId});
	}
	
	private static final String addTaskCatalogSql = "insert into TASK_CATALOG (CATALOG_ID, CATALOG_NAME, PARENT_CATALOG_ID) values(?,?,?)";
	
	private static final String updateTaskCatalogSql = "update TASK_CATALOG t set t.CATALOG_NAME = ?, T.PARENT_CATALOG_ID = ? where t.CATALOG_ID = ?";
	
	private static final String queryTaskCatalogsSql = "select CATALOG_ID, CATALOG_NAME, PARENT_CATALOG_ID from TASK_CATALOG where PARENT_CATALOG_ID = ?";
	
	private static final String queryTaskCatalogSql = "select CATALOG_ID, CATALOG_NAME, PARENT_CATALOG_ID from TASK_CATALOG where CATALOG_ID = ?";
	
	private static final String deleteTaskCatalogSql= "delete from TASK_CATALOG where CATALOG_ID = ?";
}
